.386
.model flat, stdcall
option casemap :none

	include	\masm32\include\windows.inc
	include \masm32\include\kernel32.inc
	include \masm32\include\user32.inc

	includelib \masm32\lib\kernel32.lib
	includelib \masm32\lib\user32.lib

	WinMain proto :DWORD, :DWORD, :DWORD, :DWORD
	WndProc proto :DWORD, :DWORD, :DWORD, :DWORD

.data
	ClassName	db "SINGLE_INSTANCE_APP", 0
	AppName		db "Пользовательское диалоговое окно", 0

.data?
	hInstance	HINSTANCE ?
	CommandLine	LPSTR ?

.const
	IDD_SINGLEINSTANCE_DIALOG	equ 2001

.code
start:
	invoke	GetModuleHandle, NULL
	mov	hInstance, eax
	invoke	GetCommandLine
	mov	CommandLine, eax
	invoke	WinMain, hInstance, NULL, CommandLine, SW_SHOWDEFAULT
	invoke	ExitProcess, eax

WinMain proc hInst:HINSTANCE, hPrevInst:HINSTANCE, CmdLine:LPSTR, CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:HWND

	mov	wc.cbSize, sizeof WNDCLASSEX
        mov     wc.style, CS_BYTEALIGNWINDOW or CS_SAVEBITS or CS_DBLCLKS
        mov     wc.lpfnWndProc, offset WndProc
        mov     wc.cbClsExtra, NULL
        mov     wc.cbWndExtra, DLGWINDOWEXTRA
        push    hInstance
        pop     wc.hInstance
        mov     wc.hbrBackground, COLOR_BTNFACE + 1
        mov     wc.lpszMenuName, NULL
        mov     wc.lpszClassName, offset ClassName
        invoke  LoadIcon, NULL, IDI_ASTERISK
        mov     wc.hIcon, eax
        mov     wc.hIconSm, eax
        invoke  LoadCursor, NULL, IDC_HAND
        mov     wc.hCursor, eax
        invoke  RegisterClassEx, addr wc
	invoke	CreateDialogParam, hInst, IDD_SINGLEINSTANCE_DIALOG, NULL, addr WndProc, NULL
	mov     hwnd, eax

	invoke	ShowWindow, hwnd, SW_SHOWNORMAL
	invoke	UpdateWindow, hwnd

	.while	TRUE
		invoke	GetMessage, addr msg, NULL, 0, 0
        .break	.if(!eax)
		invoke	TranslateMessage, addr msg
		invoke	DispatchMessage, addr msg
        .endw
        mov     eax, msg.wParam
        ret

WinMain endp

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
	.if uMsg == WM_CREATE
	.elseif uMsg == WM_COMMAND
		.if wParam == 2
			invoke  PostQuitMessage, NULL
		.endif
	.elseif uMsg == WM_LBUTTONDBLCLK
		invoke MessageBox, hWnd, addr AppName, addr ClassName, 0
	.elseif uMsg == WM_CLOSE
		invoke  PostQuitMessage, NULL
	.else
		invoke	DefWindowProc, hWnd, uMsg, wParam, lParam
		ret
	.endif
	xor	eax, eax
	ret
WndProc endp

end start
